package pa.iscde.core.service;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ITextEditor;

public class EditorServices {

	public static final String EDITOR_ID = "pa.iscde.core.editor.javaeditor";
	
	public static void openFile(File file) {
		if(file.exists() && file.isFile()) {
		    IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI());
		    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		 
		    try {
		        IDE.openEditorOnFileStore(page, fileStore);
		    } catch ( PartInitException e ) {
		        //Put your exception handler here if you wish to
		    }
		} else {
		   //TODO error //Do something if the file does not exist
		}
	}
	
	public static void selectText(File file, int offset, int length) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI());
		ITextEditor editor = null;
		try {
			editor = (ITextEditor) IDE.openEditorOnFileStore(page, fileStore);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		editor.selectAndReveal(offset, length);	
	}
	
	public static void selectLine(int number) {
	
		IWorkspace workspace = ResourcesPlugin.getWorkspace(); 
		IResource root = workspace.getRoot(); 
		IMarker marker;
		try {
			marker = root.createMarker("pt.iscte.pidesco.javaeditor.marker");
			
			
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		marker.setAttribute(IMarker.MESSAGE, "Buy groceries"); 
		//$NON-NLS-1$ 
//		marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); 
	}
	
	public static File getOpenFile() {
		return null;
	}
	
	public static void parseFile(File file, ASTVisitor visitor) {
		
	}
}
